import static java.lang.Math.cos;
import static java.lang.Math.sin;
import static java.lang.Math.toRadians;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.Timer;


public class ClockIcon implements Icon {
	private int _width;
	private int _height;
	
	private GregorianCalendar _systemTime;;
	private int _centerX = 200 ;
	private int _centerY = 200;
	private BufferedImage _clockFace ;
	
	public ClockIcon(int width, int height) {
		_width = width;
		_height = height;
	}
	
	@Override
	public int getIconHeight() {
		return _height;
	}

	@Override
	public int getIconWidth() {
		return _width;
	}

	@Override
	public void paintIcon(Component arg0, Graphics g, int x, int y) {
		// prendo l'ora di sistema
		_systemTime = new GregorianCalendar();
		
		/*
		int h = g.get(GregorianCalendar.HOUR);
		int m = g.get(GregorianCalendar.MINUTE);
		int s = g.get(GregorianCalendar.SECOND);
		
		arg1.drawString(h + ":" + m + ":" + s, 50, 50);*/
		
		Graphics2D graphics = (Graphics2D) g;
		
		graphics.setRenderingHint(
				RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON
		);

		if (_clockFace == null) {
			_clockFace = new BufferedImage(400, 400, BufferedImage.BITMASK);
			
			Graphics2D face = _clockFace.createGraphics();
			face.setRenderingHint(
					RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON
			);
			
			drawTiks(face);

			BasicStroke width = new BasicStroke(3);
			face.setStroke(width);
			face.setPaint(Color.BLACK);
			//face.drawOval(0, 0, 400, 400);
			drawHands(face, 0, 0, 0, 10);
		}

		graphics.drawImage(_clockFace, null, 0, 0);
		drawSecond(graphics);
		drawMinute(graphics);
		drawHour(graphics);
	}
	
	private void drawSecond(Graphics2D graphics){
		int seconds = _systemTime.get(Calendar.SECOND);
		int secondsAngle = seconds*6;
		drawHands(graphics, -30, 160, secondsAngle,1);
	}

	private void drawMinute(Graphics2D graphics){
		int minutes = _systemTime.get(Calendar.MINUTE);
		double minuteAngle = (minutes)*6;
		drawHands(graphics, 0, 150, minuteAngle,2);
	}

	private void drawHour(Graphics2D graphics){
		int hours = _systemTime.get(Calendar.HOUR);
		double hourAngle = (hours+ (double)(_systemTime.get(Calendar.MINUTE))/60)*30;
		drawHands(graphics, 0, 110, hourAngle,4);
	}

	private void drawTiks(Graphics2D graphics){
		int x=0;
		
		while(true) {
			int y =180;
			if(x%30==0)y=170;
			drawHands(graphics, y, 180, x,2);
			if (x==360) break;
			x += 6;
		}
	}

	//radius Measured From the Center of the Clock
	private void drawHands(Graphics2D graphics, int startRadius, int endRadius, double teta,int width){

		double radian = toRadians(teta-90);
		int startX = _centerX+ (int)(startRadius*cos(radian));
		int startY = _centerY+ (int)(startRadius*sin(radian));
		int endX = _centerX+ (int)(endRadius*cos(radian));
		int endY = _centerY+ (int)(endRadius*sin(radian));

		BasicStroke line = new BasicStroke(width,BasicStroke.JOIN_ROUND,BasicStroke.CAP_ROUND);
		graphics.setStroke(line);
		graphics.setPaint(Color.BLACK);
		graphics.drawLine(startX, startY, endX, endY);
	}
}